#include <iostream>

#define MAXSIZE 100

using namespace std;

struct CONNODE
{
	int adjvex;
	CONNODE* nextconnect;
};

struct VNODE
{
	int data;
	CONNODE* firstconnect ;
};

struct AGRAPH
{
	VNODE adjlist[MAXSIZE];
	int n,e;
};

AGRAPH* graph;

void insertnode(CONNODE* node, CONNODE* newnode)
{
	CONNODE* p=node;
	while(p->nextconnect!=NULL)
	{
		p=p->nextconnect;
	}
	p->nextconnect=newnode;
}

void create()
{
	graph=(AGRAPH* )new AGRAPH;
	cout<<"enter the number of nodes"<<endl;
	cin>>graph->n;
	cout<<"enter the number of lines"<<endl;
	cin>>graph->e;

	int u=-1,v=-1;
	for(int i=0;i<graph->n;i++)
	{
		graph->adjlist[i].firstconnect=NULL;
	}

	CONNODE* node;
	for(int i=0;i<graph->e;i++)
	{
		cout<<"enter the connect node A and B"<<endl;
		cin>>u>>v;
		node=(CONNODE*)new CONNODE;
		node->adjvex=v;
		node->nextconnect=NULL;
		graph->adjlist[u].data=u;
		if(graph->adjlist[u].firstconnect==NULL)
		{
			graph->adjlist[u].firstconnect=node;
		}
		else
		{
			insertnode(graph->adjlist[u].firstconnect,node);
		}
	}
}

void travsetree()
{
	for(int i=0;i< graph->n;i++)
	{
		if(graph->adjlist[i].firstconnect!=NULL)
		{
			cout<<"connect "<<i<<"nodes are:"<<endl;
			CONNODE* p=graph->adjlist[i].firstconnect;
			while(p!=NULL)
			{
				cout<<p->adjvex<<"  ";
				p=p->nextconnect;
			}
			cout<<endl;
		}
	}
}

int main(void)
{
	create();
	travsetree();

	system("pause");
	return 0;
}
				
